package com.linzm.leetcode.primary.exercises2_20230116;

import java.util.HashMap;

/**
 * @Author zimingl
 * @Date 2023/1/21 21:42
 * @Description: 将所有数字用字符替换
 */
public class Demo17_1844 {
    /**
     * 输入：s = "a1b2c3d4e"
     * 输出："abbdcfdhe"
     * 解释：数字被替换结果如下：
     * - s[1] -> shift('a',1) = 'b'
     * - s[3] -> shift('b',2) = 'd'
     * - s[5] -> shift('c',3) = 'f'
     * - s[7] -> shift('d',4) = 'h'
     */
    public static void main(String[] args) {

        String s = "a1b2c3d4e";
        Demo17_1844 demo17_1844 = new Demo17_1844();
        String replaceDigits = demo17_1844.replaceDigits2(s);
        System.out.println(replaceDigits);
    }

    private String replaceDigits(String s) {
        Character[] nums = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
                'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        String replaceDigits = "";
        Integer index = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i % 2 != 0) {
                replaceDigits += nums[index + s.charAt(i) - 48];
            } else {
                replaceDigits += s.charAt(i);
                index = map.get(s.charAt(i));
            }
        }
        return replaceDigits;
    }

    private String replaceDigits2(String s) {
        char[] chars = s.toCharArray();
        for (int i = 1; i < chars.length; i += 2) {
            chars[i]  = (char) (chars[i - 1] - '0' + chars[i]);
        }
        return new String(chars);
    }
}
